#include "systemtray.h"
#include <QApplication>
#include <QDebug>


#include "ui/configdialog.h"

SystemTray::SystemTray(QObject *parent) :
		QObject(parent),
		m_pIcon(NULL),
		m_pMenu(NULL)

{
}

SystemTray::~SystemTray(){
	delete m_pIcon;
	delete m_pMenu;
}

bool SystemTray::showIcon(){
	if( QSystemTrayIcon::isSystemTrayAvailable() ){
		m_pIcon = new QSystemTrayIcon();
		m_pIcon->setIcon(QIcon(":/icon.svg"));

		m_pMenu = new QMenu();


		QAction *action = m_pMenu->addAction(QIcon(":/res/help-about.png"),
											 tr("O serwerze"));

	//	connect( action, SIGNAL(triggered()), qApp, SLOT(quit()));


		//option action
		action = m_pMenu->addAction(QIcon(":/res/preferences-system.png"),
									tr("Preferencje"));


		connect( action, SIGNAL(triggered()), this, SLOT(showConfig()));


		//exit action
		m_pMenu->addSeparator();

		action = m_pMenu->addAction(QIcon(":/res/application-exit.png"),
									tr("Wyjście"));

		connect( action, SIGNAL(triggered()), qApp, SLOT(quit()));

		//add menu to icon
		m_pIcon->setContextMenu(m_pMenu);

		m_pIcon->setToolTip(tr("Serwer"));

		m_pIcon->show();

		return true;
	}

	return false;
}

void SystemTray::showConfig(){
	ConfigDialog *dialog = new ConfigDialog();

	connect(dialog, SIGNAL(closed()), dialog, SLOT(deleteLater()));
	connect(qApp, SIGNAL(aboutToQuit()), dialog, SLOT(deleteLater()));

	dialog->show();
}
